home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / Alphabet_Element.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  2.2 KB  |  86 lines  |  [TEXT/KAHL]

  1.  
  2. #define KEEP_ALPHABET_DEFINES
  3.  
  4. #include "Alphabet.h"
  5. #include "Alphabet_Private.h"
  6.  
  7. //________________________________________________________________________
  8.  
  9. Alphabet& Alphabet::operator+= (const char element)
  10. {
  11.     SET_ELT(element);                                                    // set the element's bit in the buffer
  12.     return *this;
  13. }
  14.  
  15. //________________________________________________________________________
  16.  
  17. Alphabet& Alphabet::operator-= (const char element)
  18. {
  19.     CLR_ELT(element);                                                // clear the element's bit in the buffer
  20.     return *this;
  21. }
  22.  
  23. //________________________________________________________________________
  24.  
  25. int Alphabet::contains (const char element) const
  26. {
  27.     return ELT(element);                                                // test the element's bit in the buffer
  28. }
  29.  
  30. //________________________________________________________________________
  31.  
  32. Alphabet& Alphabet::through(int (*action)(const char, Alphabet&), direction_t dir)
  33. {
  34.     Alphabet copy(*this);
  35.     register int byte, bit;                                    // the byte, resp. bit-counters
  36.     register unsigned char elt8;                            // the current buffer-byte
  37.     register char element;                                    // the current element
  38.     int stop = 0;
  39.     
  40.     if (dir == FORWARD)
  41.     {
  42.         for (element=ELT_LOW, byte=0; (! stop) && (byte < BUFSIZE); byte++)
  43.             if ((elt8 = copy.buffer[byte]) != 0)
  44.                 for (bit=0; (! stop) && (bit < 8); bit++, element++)
  45.                 {
  46.                     if ((elt8 & (1 << bit)) != 0)
  47.                         stop = action(element, *this);
  48.                 }
  49.             else
  50.                 element += 8;
  51.     }
  52.     else
  53.     {
  54.         for (element=ELT_HIGH, byte=BUFSIZE-1; (! stop) && (byte >= 0); byte--)
  55.             if ((elt8 = copy.buffer[byte]) != 0)
  56.                 for (bit=7; (! stop) && (bit >= 0); bit--, element--)
  57.                 {
  58.                     if ((elt8 & (1 << bit)) != 0)
  59.                         stop = action(element, *this);
  60.                 }
  61.             else
  62.                 element -= 8;
  63.     }
  64.  
  65.     return (*this);
  66. }
  67.  
  68. //________________________________________________________________________
  69.  
  70. unsigned int Alphabet::card (void) const
  71. {
  72.     unsigned int Number_Of_Elements = 0;
  73.     register int byte, bit;                                    // the byte, resp. bit-counters
  74.     register unsigned char elt8;                            // the current buffer-byte
  75.     
  76.     for (byte=0; byte < BUFSIZE; byte++)
  77.         if ((elt8 = buffer[byte]) != 0)
  78.             for (bit=0; bit < 8; bit++)
  79.             {
  80.                 if ((elt8 & (1 << bit)) != 0)
  81.                     Number_Of_Elements++;
  82.             }
  83.  
  84.     return Number_Of_Elements;
  85. }
  86.